﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Bxas.Utility.Web.Keepers
{
    public partial class SessionKeeper
    {

        #region Session Adları

        #region Enum functions

        public static string get_name(Enum name) { return name.ToString(); }
        public static int get_hash_code(Enum name) { return name.GetHashCode(); }
        public static string get_hash_code_s(Enum name) { return name.GetHashCode().ToString(); }

        #endregion

        #endregion

        /// <summary>
        /// Verilen kaydı sessiona ekler yada üzerine yazar
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public static void add(Enum name, object value)
        {
            if (contain(name)) HttpContext.Current.Session[get_name(name)] = value;
            else HttpContext.Current.Session.Add(get_name(name), value);
        }

        /// <summary>
        /// Eğer sessionda bu kayıt yok ise ekler. Varsa eklemez
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool add_is_not_contain(Enum name, object value)
        {
            if (contain(name)) return false;

            HttpContext.Current.Session.Add(get_name(name), value);
            return true;
        }

        /// <summary>
        /// Sesionda bulunan kayıt kaldırılır.
        /// </summary>
        /// <param name="name"></param>
        public static void remove(Enum name)
        {
            if (contain(name))
                HttpContext.Current.Session.Remove(get_name(name));
        }

        /// <summary>
        /// Session içerisinde gönderilen isimde kayıt varmı kontrol edilir.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool contain(Enum name)
        {
            return HttpContext.Current.Session[get_name(name)] != null;
        }

        /// <summary>
        /// Tüm sessionları siler
        /// </summary>
        public static void clear_all()
        {
            HttpContext.Current.Session.Clear();
        }

        /// <summary>
        /// Kullanıcı haricinde bütün sessionları siler.
        /// Giriş engelenmez.
        /// </summary>
        public static void clear_custom(List<Enum> names)
        {
            if (names.Count.Equals(0)) return;
            string[] allnames = Enum.GetNames(names[0].GetType());

            for (int i = 0; i < HttpContext.Current.Session.Keys.Count; i++)
            {
                if (allnames.Contains(HttpContext.Current.Session.Keys[i])) continue;

                HttpContext.Current.Session.Remove(HttpContext.Current.Session.Keys[i]);
                i--;
            }
        }

        public static object get_value(Enum name)
        {
            if (contain(name))
                return HttpContext.Current.Session[get_name(name)];
            return null;
        }

    }
}